package cn.zy.framework.util;

import cn.zy.framework.bean.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: Administrator
 * @Description:
 * @Date: Created in 17:08 2021/9/10
 * @Modified By:
 * @version: 1.0
 * 反射工具类
 */
public final class ReflectionUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);

	/**
	 * 创建实例
	 * @param cls
	 * @return
	 */
	public static Object newInstance(Class<?> cls){
		Object instance;
		try {
			instance = cls.newInstance();
		} catch (Exception e) {
			LOGGER.error("new instance failed", e);
			throw new RuntimeException(e);
		}
		return instance;
	}

	/**
	 * 调用方法
	 * @param obj
	 * @param method
	 * @param args
	 * @return
	 */
	public static Object invokeMethod(Object obj, Method method, Object... args){

		Object result;
		try {
			method.setAccessible(true);
			result = method.invoke(obj, args);
		} catch (Exception e) {
			LOGGER.error("invoke method failed", e);
			throw new RuntimeException(e);
		}
		return result;
	}

//	public static Object invokeMethod(Object obj, Method method, Param param){
//
//		Object result;
//		try {
//			method.setAccessible(true);
//			result = method.invoke(obj, param.getParamMap().values().toArray());
//		} catch (Exception e) {
//			LOGGER.error("invoke method failed", e);
//			throw new RuntimeException(e);
//		}
//		return result;
//	}
	/**
	 * 设置成员变量的值
	 * @param obj
	 * @param field
	 * @param value
	 */
	public static void setField(Object obj, Field field, Object value){

		try {
			field.setAccessible(true);
			field.set(obj, value);
		} catch (IllegalAccessException e) {
			LOGGER.error("set field failed", e);
			throw new RuntimeException(e);
		}
	}

}
